package com.summer.data.type;

import com.summer.data.dialect.Dialect;
import com.summer.data.logging.Logger;
import com.summer.data.logging.LoggerFactory;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 345480567@qq.com
 */
public class NocaseMetaData {

    private static Logger logger = LoggerFactory.getLogger(NocaseMetaData.class);

    Map<String, Integer> map = new HashMap<>();

    public NocaseMetaData(ResultSetMetaData metaData, Dialect dialect) throws SQLException {
        int columnCount = metaData.getColumnCount();
        for (int i=1; i<=columnCount; i++) {
            String columnName = dialect.isResultUseColumnName() ? metaData.getColumnName(i) : metaData.getColumnLabel(i);
            map.put(columnName.toUpperCase(), i);
        }
    }

    public Integer findColumn(String columnName) {
        String upperCase = columnName.toUpperCase();
        Integer integer = map.get(upperCase);
        if (integer == null) {
            logger.warn("ResultSet中没有找到期望返回的属性：%s", columnName);
            return null;
        } else {
            return integer;
        }
    }
}
